/**
 * Copyright (C) 2013 Denatics Development
 * 
 * Programmed by David Tiersch <dtiersch@denatics.com>
 * 
 * This file is part of B0mberman.
 * 
 * B0mberman is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * B0mberman is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with B0mberman. If not, see <http://www.gnu.org/licenses/>.
 */
package com.denatics.bomberman.control;

/**
 * This Object stores the keys, which controls the gameplay.
 * 
 * @author David Tiersch <dtiersch@denatics.com>
 */
public final class PlayerControl {

    /**
     * Move Up.
     */
    private int moveup;

    /**
     * Move Down.
     */
    private int movedown;

    /**
     * Move Left.
     */
    private int moveleft;

    /**
     * Move Right.
     */
    private int moveright;

    /**
     * Place Portal 1.
     */
    private int portal1;

    /**
     * Place Portal 2.
     */
    private int portal2;

    /**
     * Place Bomb.
     */
    private int bomb;

    /**
     * Instantiates the Keys for the current Player.
     * 
     * @param playerid
     *            ID of the Player to instantiate
     */
    public PlayerControl(final int playerid) {
        this.moveup = DefaultKeys.MOVEUP[playerid];
        this.movedown = DefaultKeys.MOVEDOWN[playerid];
        this.moveleft = DefaultKeys.MOVELEFT[playerid];
        this.moveright = DefaultKeys.MOVERIGHT[playerid];
        this.portal1 = DefaultKeys.PLACEPORTAL1[playerid];
        this.portal2 = DefaultKeys.PLACEPORTAL2[playerid];
        this.bomb = DefaultKeys.PLACEBOMB[playerid];
    }

    /**
     * Realigns the Key to a function.
     * 
     * @param type
     *            What happens at the keypress
     * @param keyCode
     *            key which should be changed
     */
    public void setKey(final Type type, final int keyCode) {
        switch (type) {
        case UP:
            this.moveup = keyCode;
            break;
        case DOWN:
            this.movedown = keyCode;
            break;
        case LEFT:
            this.moveleft = keyCode;
            break;
        case RIGHT:
            this.moveright = keyCode;
            break;
        case PORTAL1:
            this.portal1 = keyCode;
            break;
        case PORTAL2:
            this.portal2 = keyCode;
            break;
        default:
            this.bomb = keyCode;
        }
    }

    /**
     * Returns the Object that matches the Keycode.
     * 
     * @param keyCode
     *            Keycode to be looked after
     * 
     * @return Type-Object
     */
    public Type getElement(final int keyCode) {
        if (this.moveup == keyCode) {
            return Type.UP;
        }
        if (this.movedown == keyCode) {
            return Type.DOWN;
        }
        if (this.moveleft == keyCode) {
            return Type.LEFT;
        }
        if (this.moveright == keyCode) {
            return Type.RIGHT;
        }
        if (this.portal1 == keyCode) {
            return Type.PORTAL1;
        }
        if (this.portal2 == keyCode) {
            return Type.PORTAL2;
        }
        if (this.bomb == keyCode) {
            return Type.BOMB;
        }
        return null;
    }

    /**
     * Enumeration which stores the Keys.
     * 
     * @author dtiersch
     */
    public enum Type {
        /**
         * Move Upwards.
         */
        UP(0, -1),
        /**
         * Move Downwards.
         */
        DOWN(0, 1),
        /**
         * Move Left.
         */
        LEFT(-1, 0),
        /**
         * Move Right.
         */
        RIGHT(1, 0),
        /**
         * Place Bomb.
         */
        BOMB,
        /**
         * Create Portal 1.
         */
        PORTAL1,
        /**
         * Create Portal 2.
         */
        PORTAL2;

        /**
         * Change of the x-Axis when moving in this direction.
         */
        private final int dx;

        /**
         * Change of the y-Axis when moving in this direction.
         */
        private final int dy;

        /**
         * Creates the Movement-Type with the proper changes for the
         * coordinates.
         * 
         * @param xChange
         *            change on the x-axis
         * @param yChange
         *            change on the y-axis
         */
        private Type(final int xChange, final int yChange) {
            this.dx = xChange;
            this.dy = yChange;
        }

        /**
         * Creates the Action-Type.
         */
        private Type() {
            this.dx = 0;
            this.dy = 0;
        }

        /**
         * Shows how the coordinates are changed, when moving in this direction.
         * 
         * @return relative Change of coordinates
         */
        public int[] getChange() {
            final int[] change = { this.dx, this.dy };
            return change;
        }
    }
}
